package com.b2b.controller;

import com.b2b.exception.CustomException;
import com.b2b.model.BiddingDto;
import com.b2b.model.BiddingRecordDto;
import com.b2b.model.CompanyApiAuthorityDto;
import com.b2b.model.common.BaseDto;
import com.b2b.model.common.ResponseBean;
import com.b2b.model.entity.Bidding;
import com.b2b.model.entity.BiddingRecord;
import com.b2b.model.receive.CompanyId;
import com.b2b.model.receive.MyBidding;
import com.b2b.model.receive.MyBiddingRecord;
import com.b2b.util.BiddingUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/bidding")
@PropertySource("classpath:config.properties")
public class BiddingController {
    @Value("${refreshTokenExpireTime}")
    private String refreshTokenExpireTime;

    private final BiddingUtil biddingUtil;
    public BiddingController(BiddingUtil biddingUtil){
        this.biddingUtil =biddingUtil;
    }

    @PostMapping("/addBidding")
    public ResponseBean add(@RequestBody Bidding bidding){
        bidding.setDelayPeriod(5);
        char state = '0';
        bidding.setCurrentPrice(bidding.getStartingPrice());
        bidding.setState(state);
        Integer count = biddingUtil.insertBidding(bidding);
        if (count<=0){
            throw new CustomException("新增失败(Insert Failure)");
        }
        bidding.setBiddingId(count);
        return new ResponseBean(HttpStatus.OK.value(), "新增成功(Insert Success)", bidding);
    }

    @PutMapping("/publish")
    public ResponseBean updateBidding(@RequestBody Bidding bidding){
        if (!bidding.getState().equals('1')){
            throw new CustomException("更新失败(Insert Failure)");
        }
        Integer count = biddingUtil.updateBidding(bidding);
        if (count <= 0 ){
            throw new CustomException("更新失败(Insert Failure)");
        }
        return new ResponseBean(HttpStatus.OK.value(), "更新成功(Insert Success)", null);
    }
    @PostMapping("/addRecord")
    public ResponseBean addBiddingRecord(@RequestBody BiddingRecord biddingRecord){
        Integer count = biddingUtil.insertBiddingRecord(biddingRecord);
        if (count<=0){
            throw new CustomException("新增失败(Insert Failure)");
        }
        return new ResponseBean(HttpStatus.OK.value(), "新增成功(Insert Success)", null);
    }

    @GetMapping("/getAll")
    public ResponseBean getAll(BaseDto baseDto) {
        if (baseDto.getPage() == null || baseDto.getRows() == null) {
            baseDto.setPage(1);
            baseDto.setRows(10);
        }
        PageHelper.startPage(baseDto.getPage(), baseDto.getRows());
        List<BiddingDto> biddingList = biddingUtil.getAll();
        PageInfo<BiddingDto> selectPage = new PageInfo<BiddingDto>(biddingList);
        if (biddingList == null) {
            throw new CustomException("查询失败(Query Failure)!");
        }
        Map<String, Object> result = new HashMap<String, Object>(16);
        result.put("count", selectPage.getTotal());
        result.put("data", selectPage.getList());
        return new ResponseBean(HttpStatus.OK.value(), "查询成功(Query was successful)", result);
    }

    @GetMapping("/getMy")
    public ResponseBean getAllMy(@RequestParam Integer companyId){
        List<BiddingDto> biddingList = biddingUtil.getMyBidding(companyId);
        Map<String,Object> result = new HashMap<>();
        result.put("data",biddingList);
        result.put("count",biddingList.size());
        return new ResponseBean(HttpStatus.OK.value(), "查询成功(Select Success)", result);
    }

    @GetMapping("/getDetail")
    public ResponseBean getDetail(@RequestParam Integer biddingId){
        Bidding bidding = biddingUtil.getBidding(biddingId);
        return new ResponseBean(HttpStatus.OK.value(), "查询成功(Select Success)", bidding);
    }

    //分页
    @GetMapping("/getBiddingRecord")
    public ResponseBean getBiddingRecord(@RequestParam Integer biddingId){
        PageHelper.startPage(1, 10);
        PageHelper.orderBy("bidding_time" + " " + "desc");
        List<BiddingRecordDto> biddingRecordList = biddingUtil.getBiddingRecord(biddingId);
        PageInfo<BiddingRecordDto> selectPage = new PageInfo<BiddingRecordDto>(biddingRecordList);

        return new ResponseBean(HttpStatus.OK.value(), "查询成功(Select Success)", selectPage.getList());
    }

    @GetMapping("/getMyRecordBought")
    public ResponseBean getMyBidding(CompanyId companyMsg){
        if (companyMsg.getPage() == null || companyMsg.getRows() == null) {
            companyMsg.setPage(1);
            companyMsg.setRows(10);
        }
        PageHelper.startPage(companyMsg.getPage(), companyMsg.getRows());
        List<MyBiddingRecord> myBiddingRecordList = biddingUtil.getBiddingRecordList(companyMsg.getCompanyId());
        PageInfo<MyBiddingRecord> selectPage = new PageInfo<MyBiddingRecord>(myBiddingRecordList);
        Map<String, Object> result = new HashMap<String, Object>(16);
        result.put("count", selectPage.getTotal());
        result.put("recordList", selectPage.getList());
        return new ResponseBean(HttpStatus.OK.value(), "查询成功(Select Success)", result);
    }

    @GetMapping("/getMyBiddingRecord")
    public ResponseBean getBiddingRecord(CompanyId companyMsg){
        if (companyMsg.getPage() == null || companyMsg.getRows() == null) {
            companyMsg.setPage(1);
            companyMsg.setRows(10);
        }
        PageHelper.startPage(companyMsg.getPage(), companyMsg.getRows());
        List<BiddingDto> biddingList = biddingUtil.getMyBidding(companyMsg.getCompanyId());
        List<MyBidding> myBiddingList = biddingUtil.getMyBidding(biddingList);
        PageInfo<MyBidding> selectPage = new PageInfo<MyBidding>(myBiddingList);
        Map<String, Object> result = new HashMap<String, Object>(16);
        result.put("count", selectPage.getTotal());
        result.put("bidding", selectPage.getList());
        return new ResponseBean(HttpStatus.OK.value(), "查询成功(Select Success)", result);
    }
}
